package zhou.chs.p4_5;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements LocationListener {
    private String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    static final int MIN_TIME = 5000;       // 位置更新条件：5000 毫秒
    static final float MIN_DIST = 5;        // 位置更新条件：5 米
    LocationManager mManager;                   // 定位管理器
    Geocoder mAddr;                             //地址查询
    TextView tvLocate;                      // 显示定位信息
    TextView tvAddr;                        // 显示地址信息
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLocate = (TextView) findViewById(R.id.idtv1);
        tvAddr = (TextView) findViewById(R.id.idtv2);
        tvAddr.setMovementMethod(ScrollingMovementMethod.getInstance());
        mManager = (LocationManager) getSystemService(LOCATION_SERVICE);     // 获取系统定位管理器
        mAddr= new Geocoder(this, Locale.getDefault());  //获取位置解析
        int i = ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]);
        if (i != PackageManager.PERMISSION_GRANTED) {   //Android6.0需动态申请定位许可权限
            ActivityCompat.requestPermissions(this, permissions, 0);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();   //检测是否获得定位许可
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            tvLocate.setText("未被允许使用定位功能!");
            return;
        }
        // 开始监听位置事件
        // String best = gpsManager.getBestProvider(new Criteria(),true);
        // gpsManager.requestLocationUpdates(best, MIN_TIME, MIN_DIST, this);
        mManager.requestLocationUpdates(mManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, this);
        tvLocate.setText("正在获取定位信息...");
    }
    @Override
    protected void onPause() {
        super.onPause();
        mManager.removeUpdates(this);   // 停止监听位置事件
    }
    // 位置变化监听响应
    @Override
    public void onLocationChanged(Location location) {
        double mlatitude = location.getLatitude();
        double mlongitude = location.getLongitude();
        String strtxv = String.format("\n纬度:%.6f\n经度:%.6f",
                mlatitude,         // 纬度
                mlongitude);       // 经度
        tvLocate.setText("定位信息：\n" + strtxv);
        List<Address> locationList = null;
        try {
            locationList = mAddr.getFromLocation(mlatitude, mlongitude, 1);
            Address address = locationList.get(0);  //得到Address实例
            String s = address.getCountryName();    //得到国家名称，比如：中国
            s = s + "\n" + address.getLocality();   //得到城市名称，比如：北京市
            for (int i = 0; address.getAddressLine(i) != null; i++) {
                s = s + "\n" +  address.getAddressLine(i);//得到周边信息，包括街道名称
            }
            tvAddr.setText("地址信息：\n" + s);  //显示地址信息
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onProviderDisabled(String provider) {
        tvLocate.setText("GPS停用！");
    }
    @Override
    public void onProviderEnabled(String provider) {
        tvLocate.setText("GPS启用！");
    }
    @Override       // 定位提供者状态变化
    public void onStatusChanged(String provider, int status, Bundle extras) { }
}
